//
//  CircleOrbit.m
//  Project 5
//
//  Created by Nguyen Tan Loc on 6/20/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "CircleOrbit.h"

@implementation CircleOrbit
@synthesize _state = mState, _radius = mRadius;

-(id) initWithListCircle:(NSArray *)listCircle
{
    if (self = [super init])
    {
        mState = STATE_ACTIVE;
        mOrbitIndicator = [[OrbitIndicator alloc] initWithSegment:30];
        mCircleLayer = [[CCNode alloc] init];
        mCircleSafeZoneLayer = [[CCNode alloc] init];
        
        [self addChild:mOrbitIndicator];
        [self addChild:mCircleLayer];
        [self addChild:mCircleSafeZoneLayer];
        
        Circle *circle;
        for (int i = 0; i < listCircle.count; i++)
        {
            circle = (Circle *)[listCircle objectAtIndex:i];
            [mCircleLayer addChild:circle z:0 tag:i];
        }
    }
    
    return self;
}

-(void) addListCircleSafeZone:(NSArray *)listCircleSafeZone
{
    CircleSafeZone *safeZone;
    for (int i = 0; i < listCircleSafeZone.count; i++)
    {
        safeZone = (CircleSafeZone *)[listCircleSafeZone objectAtIndex:i];
        [mCircleSafeZoneLayer addChild:safeZone z:0 tag:i];
    }
}


-(void) setStartAngle:(float)angle
{
    mAngle = angle;
}

-(void) setRadius:(float)radius
{
    mRadius = radius;
    [self updateRadius];
}

-(void) updateRadius
{
    [mOrbitIndicator setRadius:mRadius];
    
    Circle *circle;
    for (int i = 0; i < mCircleLayer.children.count; i++)
    {
        circle = (Circle *)[mCircleLayer getChildByTag:i];
        [circle setPosition:ccp(mRadius * sinf(CC_DEGREES_TO_RADIANS(circle._angle)), mRadius * cosf(CC_DEGREES_TO_RADIANS(circle._angle)))];
    }
    
    CircleSafeZone *safeZone;
    for (int i = 0; i < mCircleSafeZoneLayer.children.count; i++)
    {
        safeZone = (CircleSafeZone *)[mCircleSafeZoneLayer getChildByTag:i];
        [safeZone setRadius:mRadius];
    }
}

-(void) setNextRadius:(float)nextRadius
{
    mNextRadius = nextRadius;
    if (mNextRadius != mRadius)
    {
        [self unscheduleUpdate];
        [self scheduleUpdate];
    }
}

-(void) setSpeed:(float)speed
{
    mSpeed = speed;
    
    CCRotateBy *action = [CCRotateBy actionWithDuration:1.0f angle:mSpeed]; // co the tran so do khong minimize m_Sprite.rotation
    CCRepeatForever *repeat = [CCRepeatForever actionWithAction:action];
    [mCircleLayer runAction:repeat];
}

-(void) setCheckCollision:(BOOL)b
{
    Circle *circle;
    for (int i = 0; i < mCircleLayer.children.count; i++)
    {
        circle = (Circle *)[mCircleLayer getChildByTag:i];
        [circle setCheckCollision:b];
    }
}

-(CIRCLE_ORBIT_STATE) getState
{
    if (mState == STATE_DESTROYED) return mState;
    
    mState = STATE_DESTROYING;
    for (int i = 0; i < mCircleLayer.children.count; i++)
    {
        Circle *circle = (Circle *)[mCircleLayer getChildByTag:i];
        if ([circle isDestroy] == NO)
        {
            mState = STATE_ACTIVE;
            break;
        }
    }
    
    if (mState == STATE_DESTROYING)
    {
        mState = STATE_DESTROYED;
        return STATE_DESTROYING;
    }
    else
        return mState;
}

-(void) update:(ccTime)delta
{
    mRadius -= [LevelManager shareLevelManager]._speedZoom * delta;
    if (mRadius <= mNextRadius)
    {
        mRadius = mNextRadius;
        [self unscheduleUpdate];
    }
    
    [self updateRadius];
}

-(void) runEffect:(EFFECT_TYPE)effect
{
    [mOrbitIndicator runEffect:effect];
    
    for (int i = 0; i < mCircleLayer.children.count; i++)
    {
        Circle *circle = (Circle *)[mCircleLayer getChildByTag:i];
        [circle runEffect:effect];
    }

    for (int i = 0; i < mCircleSafeZoneLayer.children.count; i++)
    {
         CircleSafeZone *safeZone = (CircleSafeZone *)[mCircleSafeZoneLayer getChildByTag:i];
        [safeZone runEffect:effect];
    }
    
    if (effect == EFFECT_DISAPPEAR)
    {
        CCDelayTime *delay = [CCDelayTime actionWithDuration:TIME_DISAPPEAR];
        CCCallFunc *func = [CCCallFunc actionWithTarget:self selector:@selector(invisible)];
        CCSequence *seq = [CCSequence actionOne:delay two:func];
        [self runAction:seq];
    }
}

-(void) invisible
{
    self.visible = false;
}

-(int) getNumCircle
{
    return mCircleLayer.children.count;
}

-(Circle *) getCircleAtIndex:(int)index
{
    if (index >= mCircleLayer.children.count) CCLOG(@"--- function (getCircleAtIndex): index is out of range");
    return (Circle *)[mCircleLayer getChildByTag:index];
}

-(void) dealloc
{
    [super dealloc];
    
    [mOrbitIndicator release];
    mOrbitIndicator = nil;
    
    [mCircleSafeZoneLayer removeAllChildrenWithCleanup:YES];
    [mCircleSafeZoneLayer release];
    mCircleSafeZoneLayer = nil;
    
    [mCircleLayer removeAllChildrenWithCleanup:YES];
    [mCircleLayer release];
    mCircleLayer = nil;
}

@end
